package slidingWindowAlgorithm;

import java.util.HashMap;
import java.util.Map;

public class Class567 {

        public boolean checkInclusion(String s1, String s2) {
            Map<Character, Integer> need = new HashMap<>();
            for(char c : s1.toCharArray())
                need.put(c, need.getOrDefault(c, 0) + 1);
            Map<Character, Integer> window = new HashMap<>();

            int valid = 0;
            int left = 0, right = 0;
            int size1 = s2.length();

            while(right < size1) {
                char c = s2.charAt(right);
                right++;

                if(need.containsKey(c)) {
                    window.put(c, window.getOrDefault(c, 0) + 1);
                    if(window.get(c).equals(need.get(c)))
                        valid++;
                }

                int needSize = need.size();
                int size2 = s1.length();

                while(valid == needSize) {
                    if(right - left == size2)
                        return true;
                    c = s2.charAt(left);
                    left++;

                    if(need.containsKey(c)) {
                        if(window.get(c).equals(need.get(c)))
                            valid--;
                        window.put(c, window.get(c) - 1);
                    }


                }

            }
            return false;
        }

    public static void main(String[] args) {
        Class567 class567 = new Class567();
        System.out.println(class567.checkInclusion("ab", "eidbaooo"));

    }





}
